package com.xjscrm.console.service.customer.wwCustomer.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.xjscrm.common.annotation.MerId;
import com.xjscrm.console.mapper.WwCustomerMapper;
import com.xjscrm.console.mapper.WwCustomerTagsMapper;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerMatchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Component
@Slf4j
public class WwCustomerMatchServiceImpl implements WwCustomerMatchService {

    @Autowired
    private WwCustomerMapper wwCustomerMapper;
    @Autowired
    private WwCustomerTagsMapper wwCustomerTagsMapper;

    public static void main(String[] args) {
        ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<List<Integer>> split = CollUtil.split(integers, 3);
        for (List<Integer> integerList : split) {
            System.out.println(integerList);
        }
    }

    @Override
    public List<Long> matchCustomerWithAnyTag(@MerId Long merId, List<Long> anyTagIds) {
        //
        return wwCustomerTagsMapper.selectListOfCustomerIdsByTagId(merId, anyTagIds);
    }

    @Override
    public List<Long> matchCustomerWithAllTag(@MerId Long merId, List<Long> allTagIds) {
        int timeCount = 5;
        //
        if (allTagIds.size() > 10) {
            List<List<Long>> splitTagIds = CollUtil.split(allTagIds, timeCount);
            Map<Long, Integer> resultMap = new HashMap<>();
            for (List<Long> splitTagId : splitTagIds) {
                resultMap.putAll(
                        wwCustomerTagsMapper.selectMapWithGroupCountByTagIds(merId, splitTagId)
                );
            }
            return new ArrayList<>(resultMap.keySet());
        } else {
            Map<Long, Integer> longIntegerMap = wwCustomerTagsMapper.selectMapWithGroupCountByTagIds(merId, allTagIds);
            return new ArrayList<>(longIntegerMap.keySet());
        }
    }

    @Override
    public List<Long> matchCustomerWithNoneTag(@MerId Long merId) {
        return wwCustomerMapper.selectListWithNoneTag(merId);
    }

    @Override
    public List<Long> matchCustomerWithAddTime(@MerId Long merId, Integer addStartTime, Integer addEndTime) {
        return wwCustomerMapper.selectListByTimeAndRemarkAndGender(merId, addStartTime, addEndTime, null, null);
    }

    @Override
    public List<Long> matchCustomerWithHasRemark(@MerId Long merId, Boolean hasRemark) {
        return wwCustomerMapper.selectListByTimeAndRemarkAndGender(merId, null, null, hasRemark, null);
    }

    @Override
    public List<Long> matchCustomerWithGender(@MerId Long merId, Integer gender) {
        return wwCustomerMapper.selectListByTimeAndRemarkAndGender(merId, null, null, null, gender);
    }

    @Override
    public List<Long> matchCustomerWithCondition(Long merId, Integer gender, Boolean hasRemark, Integer addStartTime, Integer addEndTime) {
        return wwCustomerMapper.selectListByTimeAndRemarkAndGender(merId, addStartTime, addEndTime, hasRemark, gender);
    }
}
